// standart c libs
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// mysql libs
#include <my_global.h>
#include <mysql.h>

// udp socket processing libs
#include <arpa/inet.h>
#include <sys/socket.h>

// semaphores ipc, fork etc.
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/sem.h> 
#include <sys/shm.h>

void error (char *s);



int main (void) {

	int receiver (int *, int);
	int store (int *, int);
	pid_t pid;

	int shared_mem = shmget( IPC_PRIVATE, 100, 0700 );	//creating shared memory, size: 100 byte
	int *tmp = (int*)shmat( shared_mem, 0, 0700 );	// storing temp data in shared memory
	shmctl( shared_mem, IPC_RMID, 0 );

	int semaphore = semget( IPC_PRIVATE, 2, 0700 );	// creating 2 semaphores: send and wait
	semctl( semaphore, 0, IPC_SET, 0 );	// flashing first sem
	semctl( semaphore, 1, IPC_SET, 0 );	// flashing second sem

	pid = fork();	// checking which process is currently active

	if(pid>0){
		return receiver (tmp, semaphore);
	}
	else if(pid==0){
		return store (tmp, semaphore);
	}
	else{
		printf("Fork failed\n");
	}
	return 0;
}

int receiver (int *temp, int sem) {

	struct sembuf wait = {1, -1, 0};	
	struct sembuf send = {0, 1, 0};
	int recv_data;
	struct sockaddr_in server, client;
	char data[128];

	int socketID=socket(AF_INET, SOCK_DGRAM, 0);    //socket initialization
	if(socketID==-1){      
		error("socket");
	}

	memset ((char*) &server, 0, sizeof(server));
	server.sin_family = AF_INET;
	server.sin_port = htons (4385);         // server port
	server.sin_addr.s_addr = htonl(INADDR_ANY);

	if( bind(socketID, (struct sockaddr*)&server, sizeof(server))==-1){     // socket binding
		error("bind");
	}

	while (1) {

		temp[0] = 0;
		printf("\nWaiting for data..\n");
		fflush(stdout);

		int size = sizeof(client);
		recv_data = recvfrom(socketID, (void*)&data, sizeof(data), 0, (struct sockaddr*)&client, &size);

		if(recv_data==-1){
			error("recvfrom()");
		}

		temp[0] = atoi(data);
		printf("New temp: %s\n", data);
		fflush(stdout);
		semop( sem, &send, 1 );
		semop( sem, &wait, 1 );
	}

	close(socketID);		
	shmdt( (void*)temp );
	semctl( sem, 0, IPC_RMID );

	return 0;
}

int store (int *temp, int sem) {

	struct sembuf wait = {0, -1, 0};
	struct sembuf send = {1, 1, 0};
	int received;
	MYSQL omaKanta;
	mysql_library_init( 0, NULL, NULL );
	mysql_init( &omaKanta );

	printf("Connecting MYSQL...\n");
	if (!mysql_real_connect( &omaKanta, "127.0.0.1", "root", "Koodaus1", "Temperature", 0, NULL, 0 ))       {
		printf("Connection lost.\n");
	} 
	else 
	{
		printf("Connected.\n");
	}

	if (mysql_query(&omaKanta, "use Temperature;"))
	{
		printf ("Database unusable.\n");
		return -1;
	} 
	else 
	{
		printf ("DB is ready to use.\n");
	}

	while(1){

		semop( sem, &wait, 1 );
		received = temp[0];
		char query[255];

		sprintf(query," INSERT INTO Temp (temp) VALUES(%d)",received);
		if ( mysql_query(&omaKanta, query) ) 
		{		
			printf ("\n Data transmission failed \n");
		}
		else {
			printf("Temp received and stored into DB: %d\n", received);
		}
		semop( sem, &send, 1 );
	}
	return 0;
}

void error (char *s){
	perror (s);
	exit(1);
}


